package com.ailiwu.www.ailiwu.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.bean.ItemBean;
import com.ailiwu.www.ailiwu.utils.Logger;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;

/**
 * Author:    XiYoung
 * Version    V1.0
 * Date:      2015/12/4 000421:20
 * Description:
 * Modification  History:
 * Date         	Author        		Version        	Description
 * -----------------------------------------------------------------------------------
 * 2015/12/4 0004       XiYoung              1.0                   1.0
 * Why & What is modified:
 */
public class MyListViewAdapter extends BaseAdapter {

    private ArrayList<ItemBean.DataEntity.ItemsEntity> mDatas;
    private Context mContext;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;

    /***
     * 设置不同的Type
     * Type 1 = "post"
     * Type 2 = "collection"
     */

    private static final String TYPE_POST = "post";
    private static final String TYPE_COLLECTION = "collection";

    private static final int TYPE_POST_VIEW = 0;
    private static final int TYPE_COLLECTION_VIEW = 1;

    public MyListViewAdapter(Context context, ArrayList<ItemBean.DataEntity.ItemsEntity> mDatas) {
        this.mDatas = mDatas;
        mContext = context;
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        // 使用DisplayImageOptions.Builder()创建DisplayImageOptions
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .displayer(new RoundedBitmapDisplayer(10))   // 设置成圆角图片
                .build();
    }

    @Override
    public int getCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    @Override
    public Object getItem(int i) {

        return mDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ItemBean.DataEntity.ItemsEntity itemsEntity = mDatas.get(i);
        String type = itemsEntity.getType();

        int itemViewType = getItemViewType(i);
        PostViewHolder postViewHolder;
        CollectionViewHolder collectionViewHolder;
        if (view == null) {
            switch (itemViewType) {
                case TYPE_POST_VIEW:
                    postViewHolder = new PostViewHolder();
                    view = View.inflate(mContext, R.layout.item_recyclerview_home_first, null);
                    postViewHolder.tvUpdataTime = (TextView) view.findViewById(R.id.tv_time_updata);
                    postViewHolder.tvNextUpdataTime = (TextView) view.findViewById(R.id.tv_time_next_updata);
                    postViewHolder.tvTitle = (TextView) view.findViewById(R.id.tv_title);
                    postViewHolder.tvLikeNum = (TextView) view.findViewById(R.id.tv_liked_num);
                    postViewHolder.ivBg = (ImageView) view.findViewById(R.id.iv_image);
                    postViewHolder.cbLiked = (CheckBox) view.findViewById(R.id.checkBox_liked);
                    postViewHolder.llTop = (LinearLayout) view.findViewById(R.id.item_header);
                    view.setTag(postViewHolder);
                    break;
                case TYPE_COLLECTION_VIEW:
                    collectionViewHolder = new CollectionViewHolder();
                    view = View.inflate(mContext, R.layout.item_recyclerview_home_first_collection, null);
                    collectionViewHolder.tvTitle = (TextView) view.findViewById(R.id.tv_title);
                    collectionViewHolder.tvSubTitle = (TextView) view.findViewById(R.id.tv_subtitle);
                    collectionViewHolder.ivBg = (ImageView) view.findViewById(R.id.iv_image);
                    collectionViewHolder.llTop = (LinearLayout) view.findViewById(R.id.item_header);
                    view.setTag(collectionViewHolder);
                    break;
                default:
                    break;
            }
        } else {
            switch (itemViewType) {
                case TYPE_POST_VIEW:
                    postViewHolder = (PostViewHolder) view.getTag();
                    String title = itemsEntity.getTitle();
                    String url = itemsEntity.getCover_image_url();
                    int published_at = itemsEntity.getPublished_at();
                    int nextUpdataTime = itemsEntity.getUpdated_at() - published_at;
                    int likedNum = itemsEntity.getLikes_count();
                    boolean isLiked = itemsEntity.isLiked();

                    postViewHolder.tvTitle.setText(title);
                    postViewHolder.tvLikeNum.setText(likedNum + "");
                    postViewHolder.tvNextUpdataTime.setText(nextUpdataTime + "");
                    postViewHolder.tvUpdataTime.setText(published_at + "");
                    postViewHolder.cbLiked.setChecked(isLiked);
                    imageLoader.displayImage(url, postViewHolder.ivBg, options);
                    break;
                case TYPE_COLLECTION_VIEW:
//                    collectionViewHolder = (CollectionViewHolder) view.getTag();
//                    String title1 = itemsEntity.getTitle();
//                    String url1 = itemsEntity.getCover_image_url();
//                    String subtitle = itemsEntity.getSubtitle();
//                    int id = itemsEntity.getId();
//                    //设置布局
//                    imageLoader.displayImage(url1, collectionViewHolder.ivBg, options);
//                    collectionViewHolder.tvTitle.setText(title1);
//                    collectionViewHolder.tvSubTitle.setText(subtitle);
                    break;
            }
        }
        return view;
    }

    @Override
    public int getItemViewType(int position) {
        ItemBean.DataEntity.ItemsEntity itemsEntity = mDatas.get(position);
        String type = itemsEntity.getType();
        if (TYPE_POST.equals(type)) {

            return TYPE_POST_VIEW;
        } else if (TYPE_COLLECTION.equals(type)) {
            Logger.i(type);
            return TYPE_COLLECTION_VIEW;
        }
        return super.getItemViewType(position);
    }

    class PostViewHolder {
        TextView tvUpdataTime, tvNextUpdataTime, tvTitle, tvLikeNum;
        ImageView ivBg;
        CheckBox cbLiked;
        LinearLayout llTop;
    }

    class CollectionViewHolder {
        TextView tvTitle, tvSubTitle;
        ImageView ivBg;
        LinearLayout llTop;
    }
}
